//
//  JPhotoBrowserAnimator.swift
//  JMicroBlog
//
//  Created by Jason on 27/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit

class JPhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    
    // 是否展现
    var isPresent = false
    
    var dummyView = UIView()
    var dummyViewStart = CGRectZero
    var dummyViewEnd = CGRectZero
    // 指定实现转场的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 指定转场消失的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    
    // 指定转场时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 1.0
    }
    
    // 指定转场实现的细节
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        let during = transitionDuration(transitionContext)
        if  isPresent {
            // 找到目标视图
            if let toView = transitionContext.viewForKey(UITransitionContextToViewKey) {
                // 将目标视图添加到容器视图中
                transitionContext.containerView().addSubview(dummyView)
                // 设置动画
                // toView.alpha = 0.0
                // toView.frame = CGRectMake(0, 0, 0, 0)
                UIView.animateWithDuration(during, animations: {
                    // 目标视图占满屏幕
                    // toView.frame = UIScreen.mainScreen().bounds
                    // toView.alpha = 1.0
                    self.dummyView.frame = self.dummyViewEnd
                    }, completion: { _ in
                        //删除临时转场的视图
                        self.dummyView.removeFromSuperview()
                        // 动画完成结束后 添加目标视图 还是忘了这玩意
                        transitionContext.containerView().addSubview(toView)
                        // 结束转场动画
                        transitionContext.completeTransition(true)
                })
            }
        }else {
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            //            println(transitionContext.viewControllerForKey(UITransitionContextFromViewKey)?.classForCoder)
            let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! JPhotoBrowserVC
            
            let photoCell = fromVC.collectionView.visibleCells().last as! JPhotoBrowserCell
            let imageView = photoCell.viewerVC?.view
            
            let rect = CGRectMake(CGRectGetMidX(dummyViewStart), CGRectGetMidY(dummyViewStart), 0, 0)
            // 抓取快照
            var  dummy = imageView?.snapshotViewAfterScreenUpdates(false)
            
            // 隐藏fromView
            fromView?.hidden = true
            
            transitionContext.containerView().addSubview(dummy!)
            UIView.animateWithDuration(during, animations: {
                dummy?.frame = rect
                }, completion: { (_) -> Void in
                    transitionContext.completeTransition(true)
            })
        }
        
    }
}
